package com.weixx.yctp.cp.controller;

import cc.eamon.open.permission.annotation.Permission;
import cc.eamon.open.permission.annotation.PermissionLimit;
import cc.eamon.open.status.Status;
import cc.eamon.open.status.StatusBaseController;
import cc.eamon.open.status.StatusCode;
import com.weixx.yctp.cp.entity.PcPostMapper;
import com.weixx.yctp.cp.entity.PcUpdateMapper;
import com.weixx.yctp.cp.service.PcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.lang.Exception;
import java.lang.Integer;
import java.lang.Long;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author: eamon
 * Email: eamon@eamon.cc */
@Api(
    value = "测评批次-操作管理",
    tags = "测评批次-操作管理"
)
@RestController
@RequestMapping("/pc")
@Permission
public class PcController extends StatusBaseController {
  @Autowired
  private PcService pcService;

  @ApiOperation(
      value = "获取测评批次-操作简要列表",
      notes = "获取测评批次-操作简要列表"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getPcSimpleMapList(@RequestParam(required = false, defaultValue = "0") Long page,
      @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.getPcSimpleMapList(page, rows),
        pcService.getPcCount()
        );
  }

  @ApiOperation(
      value = "获取测评批次-操作详情列表",
      notes = "获取测评批次-操作详情列表"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "detail",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getPcDetailMapList(@RequestParam(required = false, defaultValue = "0") Long page,
      @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.getPcDetailMapList(page, rows),
        pcService.getPcCount()
        );
  }

  @ApiOperation(
      value = "通过主键获取测评批次-操作简要信息",
      notes = "通过主键获取测评批次-操作简要信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "getwithkey/{entityKey}",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getPcSimpleMap(@RequestParam Integer entityKey) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.getPcSimpleMapByPrimaryKey(entityKey),
        "查询成功");
  }

  @ApiOperation(
          value = "通过时间获取测评批次-操作简要信息",
          notes = "通过时间获取测评批次-操作简要信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "{startTime}/{endTime}",
          method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getPcSimpleMapByTime(@PathVariable String startTime,@PathVariable String endTime,@RequestParam(required = false, defaultValue = "0") Long page,
                                     @RequestParam(required = false, defaultValue = "10") Integer rows) throws Exception {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date startDate=sdf.parse(startTime);
    Date endtDate=sdf.parse(endTime);
    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            pcService.getPcSimpleMapByTime(page,rows,startDate,endtDate),
            "查询成功");
  }

  @ApiOperation(
      value = "通过主键获取测评批次-操作详细信息",
      notes = "通过主键获取测评批次-操作详细信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "/detail/{entityKey}",
      method = RequestMethod.GET
  )
  @ResponseBody
  @PermissionLimit
  public Status getPcDetailMap(@PathVariable Integer entityKey) throws Exception {
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.getPcDetailMapByPrimaryKey(entityKey),
        "查询成功");
  }

  @ApiOperation(
      value = "发布测评批次-操作信息",
      notes = "发布测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "/postpc",
      method = RequestMethod.POST
  )
  @ResponseBody
  @PermissionLimit
  public Status postPcMap(@RequestBody PcPostMapper entityPostMapper, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    userName="unlogin";

    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.postPc(entityPostMapper, userName, userName),
        "发布成功");
  }

  @ApiOperation(
      value = "发布一组测评批次-操作信息",
      notes = "发布一组测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.POST
  )
  @ResponseBody
  @PermissionLimit
  public Status postPcMapList(@RequestBody ArrayList<PcPostMapper> entityPostMapperList,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.postPcList(entityPostMapperList, userName, userName),
        "发布成功");
  }

  @ApiOperation(
      value = "更新测评批次-操作信息",
      notes = "更新测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "{entityKey}",
      method = RequestMethod.PUT
  )
  @ResponseBody
  @PermissionLimit
  public Status updatePcMap(@PathVariable Integer entityKey,
      @RequestBody PcUpdateMapper entityUpdateMapper, HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    entityUpdateMapper.id = entityKey;
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.updatePc(entityUpdateMapper, userName),
        "更新成功");
  }

  @ApiOperation(
      value = "更新一组测评批次-操作信息",
      notes = "更新一组测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.PUT
  )
  @ResponseBody
  @PermissionLimit
  public Status updatePcMapList(@RequestBody ArrayList<PcUpdateMapper> entityUpdateMapperList,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.updatePcList(entityUpdateMapperList, userName),
        "更新成功");
  }

  @ApiOperation(
      value = "删除测评批次-操作信息",
      notes = "删除测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "{entityKey}",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status deletePcByPrimaryKey(@PathVariable Integer entityKey, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.deletePc(entityKey, userName),
        "删除成功");
  }

  @ApiOperation(
      value = "删除一组测评批次-操作信息",
      notes = "删除一组测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "list",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status deletePcByPrimaryKeyList(@RequestBody ArrayList<Integer> keys,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.deletePcList(keys, userName),
        "删除成功");
  }

  @ApiOperation(
      value = "恢复测评批次-操作信息",
      notes = "恢复测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "recover/{entityKey}",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status recoverPcByPrimaryKey(@PathVariable Integer entityKey, HttpServletRequest request)
      throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.recoverPc(entityKey, userName),
        "恢复成功");
  }

  @ApiOperation(
      value = "恢复一组测评批次-操作信息",
      notes = "恢复一组测评批次-操作信息"
  )
  @Transactional(
      rollbackFor = Exception.class
  )
  @RequestMapping(
      value = "recover/list",
      method = RequestMethod.DELETE
  )
  @ResponseBody
  @PermissionLimit
  public Status recoverPcByPrimaryKeyList(@RequestBody ArrayList<Integer> keys,
      HttpServletRequest request) throws Exception {
    String userName = (String) request.getAttribute("userName");
    return new Status(
        true,
        StatusCode.getCode("SUCCESS"),
        pcService.recoverPcList(keys, userName),
        "恢复成功");
  }

  @ApiOperation(
          value = "获取正在进行的批次-查看详细信息",
          notes = "获取正在进行的批次-查看详细信息"
  )
  @Transactional(
          rollbackFor = Exception.class
  )
  @RequestMapping(
          value = "/ondoing",
          method = RequestMethod.GET
  )
  @ResponseBody
  public Status getOnDongPc() throws Exception {
    return new Status(
            true,
            StatusCode.getCode("SUCCESS"),
            pcService.getOnDongPc(),
            "查询成功");
  }
}
